﻿using System.Collections.Generic;
using System.Linq;
using Configuration;
using Model;
using Repository.Common;

namespace Repository
{
    public class ProductRepository : Repository<Product>
    {
        public ProductRepository(Database context)
            : base(context)
        {
        }

        public List<Product> GetAllHaveCategory()
        {
            GetContext().ContextOptions.LazyLoadingEnabled = true;

            var res = from r in GetQuery().Include("Category").OrderBy(o => o.Id) select r;

            return res.ToList();
        }

        public List<Product> GetAllWithRelation()
        {
            GetContext().ContextOptions.LazyLoadingEnabled = true;

            var res = from r in GetQuery().Include("Category").Include("Province").Include("District").OrderBy(o => o.Id) select r;

            return res.ToList();
        }

        public Product GetById(int id)
        {
            GetContext().ContextOptions.LazyLoadingEnabled = true;

            var res = (from r in GetQuery().Include("Category").Include("Province").Include("District").OrderBy(o => o.Id) where r.Id == id select r).SingleOrDefault();

            return res;
        }

        public List<Product> GetProductNewest(int iHowMany)
        {
            var res = (from r in GetQuery().OrderByDescending(o => o.Priority)
                       select r).Take(iHowMany).ToList();
            return res;
        }

        public List<Product> GetProductByCateID(int iCateID, int iHowMany)
        {
            var res = (from r in GetQuery().OrderByDescending(o => o.Priority)
                       where r.Category.Id == iCateID
                       select r).Take(iHowMany).ToList();
            return res;
        }

        public List<Product> GetProductByCateID(int iCateID, int iHowMany, int page)
        {
            GetContext().ContextOptions.LazyLoadingEnabled = true;

            var skipValue = (page - 1) * iHowMany;
            var res = (from r in GetQuery().Include("Images").Include("Category").OrderByDescending(o => o.Priority)
                       where r.Category.Id == iCateID && (from i in r.Images where i.IsAvatar == true select i).Count<Image>() == 1
                       select r).Skip(skipValue).Take(iHowMany).ToList();
            return res;
        }

        public List<Product> GetProductSearch(int iprovinceID, int idistrict, int iHowMany, int page)
        {
            GetContext().ContextOptions.LazyLoadingEnabled = true;

            if (iprovinceID != 0 && idistrict != 0)
            {
                var skipValue = (page - 1) * iHowMany;
                var res = (from r in GetQuery().Include("Images").Include("Category").OrderByDescending(o => o.Priority)
                           where r.Province.Id == iprovinceID && r.District.Id == idistrict && (from i in r.Images where i.IsAvatar == true select i).Count<Image>() == 1
                           select r).Skip(skipValue).Take(iHowMany).ToList();
                return res;
            }
            else
            {
                var skipValue = (page - 1) * iHowMany;
                var res = (from r in GetQuery().Include("Images").Include("Category").OrderByDescending(o => o.Priority)
                           where r.Province.Id == iprovinceID && (from i in r.Images where i.IsAvatar == true select i).Count<Image>() == 1
                           select r).Skip(skipValue).Take(iHowMany).ToList();
                return res;
            }
        }

        public int GetTotalPageSearch(int iprovinceID, int idistrict, int numOfRows)
        {
            if (iprovinceID != 0 && idistrict != 0)
            {
                var res = (from r in GetQuery().OrderByDescending(o => o.Priority)
                           where r.Province.Id == iprovinceID && r.District.Id == idistrict
                           select r).ToList();

                return base.GetTotalPage(numOfRows, res);
            }
            else
            {
                var res = (from r in GetQuery().OrderByDescending(o => o.Priority)
                           where r.Province.Id == iprovinceID
                           select r).ToList();

                return base.GetTotalPage(numOfRows, res);
            }
            //return ((int)((double)res.ToList().Count() / (double)numOfRows)) + 1;
        }

        public int GetTotalPageByCategory(int categoryId, int numOfRows)
        {
            var res = (from r in GetQuery().OrderByDescending(o => o.Priority)
                       where r.Category.Id == categoryId
                       select r).ToList();

            return base.GetTotalPage(numOfRows, res);
            //return ((int)((double)res.ToList().Count() / (double)numOfRows)) + 1;
        }

        public Product GetProductHottest()
        {
            var res = (from r in GetQuery()
                       where r.IsHot == true
                       select r).FirstOrDefault();
            return res;
        }

        public List<Product> Paging(int pageIndex, int numOfRows)
        {
            var skipRows = (pageIndex - 1) * numOfRows;

            return GetAllWithRelation().Skip(skipRows).Take(numOfRows).ToList();
        }

        public Product HaveImage(int id)
        {
            GetContext().ContextOptions.LazyLoadingEnabled = true;

            var res = (from c in GetQuery().Include("Images") where c.Id == id select c).SingleOrDefault();

            return res;
        }

        public List<Product> SearchByName(string name)
        {
            var res = from c in GetQuery() where c.Name.ToLower().Contains(name.ToLower()) select c;

            return res.ToList();
        }
    }
}